package com.renwei.yunlong.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.RegisterBean;
import com.renwei.yunlong.http.HttpPresenter;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.ToastUtil;
import com.zyf.fwms.commonlibrary.http.Api;
import com.zyf.fwms.commonlibrary.utils.AutoUtils;
import com.zyf.fwms.commonlibrary.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

import static android.R.attr.password;

/**
 * 修改密码--页面
 * <p>
 * Created by Administrator on 2017/8/25.
 */

public class ChangePasswordActivity extends BaseActivity implements View.OnClickListener, HttpTaskListener {

    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_edit)
    TextView tvEdit;
    @Bind(R.id.et_original_pwd)
    EditText etOriginalPwd;//原密码
    @Bind(R.id.et_pwd)
    EditText etPwd;//设置密码
    @Bind(R.id.et_confirm_pwd)
    EditText etConfirmPwd;//确认密码

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_change_password);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);

        //屏幕自适配
        AutoUtils.setSize(this, false, 720, 1280);
        AutoUtils.auto(this);

        initData();
    }

    private void initData() {
        ivBack.setVisibility(View.VISIBLE);
        tvEdit.setVisibility(View.VISIBLE);
        tvTitle.setText("修改密码");
        ivBack.setOnClickListener(this);
        tvEdit.setOnClickListener(this);
        tvEdit.setText("保存");
    }

    //服务商修改密码
    private void serviceUpdatePasswordHttp() {
        HttpPresenter hp = new HttpPresenter(this);
        hp.setUrl(Api.SERVICE_UPDATEPWD_URL);
        hp.setshowDialogcontent("正在提交");
        hp.setRequsetId(1);
        //hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(this);
        hp.post();
    }

    //业主修改密码
    private void ownerUpdatePasswordHttp(String jsonBean){
        HttpPresenter hp = new HttpPresenter(this);
        hp.setUrl(Api.OWNER_UPDATEPWD_URL);
        hp.setshowDialogcontent("正在提交");
        hp.setRequsetId(2);
        hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(this);
        hp.post();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_edit://保存
                String originalPwd = etOriginalPwd.getText().toString().trim();

                String confirmPwd = etConfirmPwd.getText().toString().trim();

                if (!TextUtils.isEmpty(originalPwd) && !TextUtils.isEmpty(confirmPwd)){
                    String jsonBean="{userId:\""+"c21debeb-8696-4896-8e1a-573c4305b0b7"+"\",oldpassword:\""+originalPwd+"\",userPwd:\""+confirmPwd+"\"}";
                    ownerUpdatePasswordHttp(jsonBean);
                }else {
                    ToastUtil.showShort(this,"不能为空");
                }

                break;
            default:
                break;
        }
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1:
                break;
            case 2:
                RegisterBean registerBean = new Gson().fromJson(result, RegisterBean.class);
                if (registerBean.getMessage().getCode() == 200){//成功  200
                    ToastUtil.showShort(this,"修改成功");
                    finish();
                }else {//账号或密码错误
                    ToastUtil.showShort(this,"账号或密码错误");
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onException(int requestId, String code) {

    }
}
